package cn.kgc.app.ssm.service.Impl;

import cn.kgc.app.ssm.common.ServerResponse;
import cn.kgc.app.ssm.dao.Backend_userMapper;
import cn.kgc.app.ssm.model.pojo.Backend_user;
import cn.kgc.app.ssm.service.Backend_userService;
import cn.kgc.app.ssm.util.MD5Util;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Resource;

@Service("Backend_userService")
public class Backend_userServicImpl implements Backend_userService {
    @Resource
    private Backend_userMapper backendUserMapper;
    /**
     * 用户登录
     *
     * @param userCode     账号
     * @param userPassword 密码
     * @return 返回结果
     */
    @Override
    public ServerResponse userLogin(String userCode, String userPassword) {
        ServerResponse serverResponse=null;
        //查询账号是否存在
        int rowCount=backendUserMapper.checkUserCode(userCode);
        if (rowCount<=0){
            return  new ServerResponse(-1,"用户不存在");
        }
        //加密密码
        String md5Password= MD5Util.MD5EncodeUtf8(userPassword);
        //验证密码
        Backend_user backendUser=backendUserMapper.queryUserByUserCodeAndPassword(userCode,md5Password);
        if (backendUser==null){
            return new ServerResponse(-1,"密码错误");
        }
        //登录成功后清空密码
        backendUser.setUserPassword(StringUtils.EMPTY);
        return  new ServerResponse(1,backendUser);


    }
}
